package com.smartleanx.module.hrm.service.esign.auth.impl;

import cn.hutool.core.util.StrUtil;

import com.alibaba.fastjson2.JSONObject;
import com.smartleanx.module.hrm.service.esign.auth.EsignCommonService;
import com.smartleanx.module.hrm.service.esign.temp.vo.Auth.EsignProperties;
import com.smartleanx.module.hrm.service.esign.vo.OrgOwnSealListResp;
import com.smartleanx.module.hrm.service.esign.vo.OrgSealInfo;
import com.smartleanx.module.hrm.util.esign.enums.EsignRequestType;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 印章服务（API版）
 * 
 * @author yanglihao
 */
@Slf4j
@Service
@AllArgsConstructor
public class EsignSealServiceImpl extends EsignCommonService {

    @Autowired
    private EsignProperties properties;

    /**
     * 【精简】查询企业内部印章（公章，合同章，人事章，财务章）
     *
     * @param orgId 机构账号ID
     * @param pageNum 查询页码
     */
    public OrgOwnSealListResp getOrgOwnSealList(String orgId, int pageNum) throws Exception {
        String sealBizTypes = "PUBLIC,CONTRACT,FINANCE,PERSONNEL";
        return getOrgOwnSealList(orgId, pageNum, 20, sealBizTypes);
    }



    /**
     * 【精简】查询企业内部印章（公章）
     *
     * @param orgId 机构账号ID
     * @param pageNum 查询页码
     */
    public OrgOwnSealListResp getOrgOwnSealPublic(String orgId, int pageNum) throws Exception {
        String sealBizTypes = "PUBLIC";
        return getOrgOwnSealList(orgId, pageNum, 20, sealBizTypes);
    }


    /**
     * 【通用】查询企业内部印章
     *
     * @param orgId 机构账号ID
     * @param pageNum 查询页码
     * @param pageSize 单页展示的最大数量，最大20
     * @param sealBizTypes 印章业务类型（多项可使用英文逗号分隔）
     */
    public OrgOwnSealListResp getOrgOwnSealList(String orgId, int pageNum, int pageSize, String sealBizTypes)
        throws Exception {
        pageSize = Math.min(pageSize, 20);
        String apiAddr = "/v3/seals/org-own-seal-list?orgId=" + orgId + "&pageNum=" + pageNum + "&pageSize=" + pageSize;
        if (StrUtil.isNotBlank(sealBizTypes)) {
            apiAddr += ("&sealBizTypes=" + sealBizTypes);
        }
        return baseRequest(properties, EsignRequestType.GET, apiAddr, null, OrgOwnSealListResp.class);
    }

    /**
     * 【通用】查询企业指定印章详情
     *
     * @param orgId 机构账号ID
     * @param sealId 印章编号
     */
    public OrgSealInfo getOrgSealInfo(String orgId, String sealId) throws Exception {
        String apiAddr = "/v3/seals/org-seal-info?orgId=" + orgId + "&sealId=" + sealId;
        return baseRequest(properties, EsignRequestType.GET, apiAddr, null, OrgSealInfo.class);
    }

    /**
     * 【通用】设置机构默认印章
     *
     * @param orgId 机构账号ID
     * @param sealId 新的默认印章ID
     */
    public void setOrgDefaultSeal(String orgId, String sealId) throws Exception {
        String apiAddr = "/v3/seals/org-seals/set-default-seal";
        JSONObject param = new JSONObject();
        param.put("orgId", orgId);
        param.put("sealId", sealId);
        baseRequest(properties, EsignRequestType.POST, apiAddr, param, Object.class);
    }

    /**
     * 【通用】删除机构印章
     *
     * @param orgId 机构账号ID
     * @param sealId 新的默认印章ID
     *
     * @apiNote 删除后印章相关的授权记录将会一并删除，且无法恢复<br>
     *          企业默认印章不可被删除（默认印章只有一个）<br>
     *          审核中的图片印章不可被删除<br>
     *          e签宝自动创建的非默认印章可以删除，但不会触发回调通知
     */
    public void deleteOrgSeal(String orgId, String sealId) throws Exception {
        String apiAddr = "/v3/seals/org-seal?orgId=" + orgId + "&sealId=" + sealId;
        baseRequest(properties, EsignRequestType.DELETE, apiAddr, null, Object.class);
    }
}
